﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TestJQGrid
{
    public partial class SlabStructure : System.Web.UI.Page
    {
        #region page member&method
        private static SlabControl sb;
        private static bool IsNewSlab;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblMenuName.Text = "SLABS";
                lblMenuName.Font.Bold = true;
                lblMenuName.Font.Size = 14;
                lblMenuName.ForeColor = System.Drawing.Color.Navy;
            }
        }
        #endregion

        #region data display
        protected void LBSlabs_DataBound(object sender, EventArgs e)
        {
            //set selected index to proper value
            if (!IsPostBack || LBSlabs.SelectedIndex < 0)
                LBSlabs.SelectedIndex = 0;

            //get SlabId
            int SlabId = 0;
            if (sb != null) SlabId = sb.getMatrix().getSlabId();
            else SlabId = Convert.ToInt32(LBSlabs.SelectedValue.ToString());

            //display Slab
            loadSlab2Panel(SlabId);
            setPageProperties(false);
        }

        void loadSlab2Panel(int SlabId)
        {
            sb = new SlabControl(SlabId);
            displayTB(sb.getMatrix().getMatrix2TB());
            //displayDT(sb.getMatrix().getMatrix2DT());
            return;
        }

        void displayDT(DataTable dt)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
            //panelInner.Controls.Add();
        }

        void displayTB(TextBox[,] tbSlab)
        {
            if (sb.getMatrix().getRowCount() != 1)
                tbSlab[0, 0].Text = " ";
            panelInner.Width = (sb.getMatrix().getColumnCount() + 1) * 77;
            for (int i = 0; i <= sb.getMatrix().getRowCount(); i++)
            {
                for (int j = 0; j <= sb.getMatrix().getColumnCount(); j++)
                {
                    panelInner.Controls.Add(tbSlab[i, j]);
                }
            }
            //panelInner.Controls.Add(tbSlab);
            //panelInner.
            return;
        }

        protected void LBSlabs_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SlabId = Convert.ToInt32(LBSlabs.SelectedValue.ToString());
            loadSlab2Panel(SlabId);
            setPageProperties(false);
        }

        private void setPageProperties(bool editable)
        {
            enablePage(editable);
            if (!editable) updatePageValues();
        }

        private void enablePage(bool editable)
        {
            DropDownList1.Enabled = editable;
            DropDownList2.Enabled = editable;
            DropDownList3.Enabled = editable;
            DropDownList4.Enabled = editable;
            DropDownList5.Enabled = editable;
            DDLStatus.Enabled = editable;
            ButtonApply.Visible = editable;
            ButtonCancel.Visible = editable;
            panelInner.Enabled = editable;
            TextBoxName.Enabled = editable;
            TextBoxFrom.Enabled = editable;
            TextBoxTo.Enabled = editable;
            ButtonEdit.Visible = !editable;
            ButtonCreate.Visible = !editable;
            panelInner.Enabled = editable;
            GridView1.Enabled = editable;
        }

        private void updatePageValues()
        {
            //retieve upper part
            string selectQuery = "select [SlabName],[FromDate],[ToDate],[ROperator],[COperator],[Status], [IsStepped] " +
                "from [Slabs] where [SlabId]='" + sb.getMatrix().getSlabId().ToString() + "'";
            DataTable dt = GlobalMethods.ExecuteSelectQuery(selectQuery);

            //set upper values
            TextBoxName.Text = dt.Rows[0][0].ToString();
            TextBoxFrom.Text = Convert.ToDateTime(dt.Rows[0][1].ToString()).ToString("dd-MMM-yyyy");
            TextBoxTo.Text = Convert.ToDateTime(dt.Rows[0][2].ToString()).ToString("dd-MMM-yyyy");
            DropDownList2.SelectedValue = dt.Rows[0][3].ToString();
            DropDownList4.SelectedValue = dt.Rows[0][4].ToString();
            DDLStatus.SelectedValue = dt.Rows[0][5].ToString();
            DropDownList5.SelectedValue = dt.Rows[0][6].ToString().Trim();

            //set dimensions
            DropDownList1.SelectedValue = sb.getMatrix().getRowCount().ToString();
            DropDownList3.SelectedValue = sb.getMatrix().getColumnCount().ToString();
        }
        #endregion

        #region button clicks
        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            int SlabId = sb.getMatrix().getSlabId();
            loadSlab2Panel(SlabId);
            //setPageProperties(true);
            panelInner.Enabled = true;
            GridView1.Enabled = true;
            ButtonApply.Visible = true;
            ButtonCancel.Visible = true;
            ButtonCreate.Visible = false;
            ButtonEdit.Visible = false;
            IsNewSlab = false;
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            IsNewSlab = false;
            int SlabId = sb.getMatrix().getSlabId();
            loadSlab2Panel(SlabId);
            setPageProperties(false);
        }

        protected void ButtonCreate_Click(object sender, EventArgs e)
        {
            IsNewSlab = true;
            setPageProperties(true);
            resetValues();
        }

        private void resetValues()
        {
            DropDownList1.SelectedIndex = 0;
            DropDownList2.SelectedIndex = 0;
            DropDownList3.SelectedIndex = 0;
            DropDownList4.SelectedIndex = 4;
            DropDownList5.SelectedIndex = 0;
            DDLStatus.SelectedIndex = 0;
            //TextBoxFrom.Text = Convert.ToDateTime("01/01/2011").ToString("dd/MM/yyyy");
            //TextBoxTo.Text = Convert.ToDateTime("31/12/2011").ToString("dd/MM/yyyy");
            TextBoxFrom.Text = DateTime.Now.ToString("dd-MMM-yyyy");
            TextBoxTo.Text = DateTime.Now.AddYears(1).ToString("dd-MMM-yyyy");
            TextBoxName.Text = "Untitled_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
            GridView1.DataSource = null;
            GridView1.DataBind();
        }

        private int updateSlabsTable()
        {
            //retieve values to insert
            string slabName = TextBoxName.Text.ToString();
            DateTime fromDate = Convert.ToDateTime(TextBoxFrom.Text.ToString());
            DateTime toDate = Convert.ToDateTime(TextBoxTo.Text.ToString());
            string slabType = DropDownList5.SelectedValue.ToString();
            int rowC = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
            int colC = Convert.ToInt32(DropDownList3.SelectedValue.ToString());
            string rowOp = DropDownList2.SelectedValue.ToString();
            string colOp = DropDownList4.SelectedValue.ToString();
            string status = DDLStatus.SelectedValue.ToString();
            int dimension = 1;
            if (colC > 1) dimension = 2;

            //create and execute insert command in Slabs table
            string insertCommand = "INSERT INTO Slabs(SlabName,ProductName,FromDate,ToDate,SlabType,ROperator,COperator,Status,IsStepped,CreatedOn,CreatedBy)" +
                "VALUES('" + slabName + "','"+Session["ProductName"].ToString()+"','" + fromDate.ToString("yyyy-MM-dd") + "','" + toDate.ToString("yyyy-MM-dd") + "','" + dimension.ToString() + "','" + rowOp + "','" +
                colOp + "','" + status + "','" + slabType + "',getDate(),'"+Session["loginId"].ToString()+"'" + ") ";
            int slab_id = GlobalMethods.ExecuteIdentityInsert(insertCommand, "SlabId");

            return slab_id;
        }


        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.DataBind();
        }
        #endregion

        protected void ButtonApply_Click(object sender, EventArgs e)
        {
            if (IsNewSlab)
            {
                //retrieve dimension
                int slab_id = updateSlabsTable();
                int rows = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
                int cols = Convert.ToInt32(DropDownList3.SelectedValue.ToString());

                //update db
                sb = new SlabControl(slab_id, rows, cols);
                sb.getMatrix().updateDB();

                //control display
                LBSlabs.DataBind();
                LBSlabs.SelectedValue = slab_id.ToString();
                IsNewSlab = false;
                return;
            }
            else
            {
                TextBox[,] tb2 = new TextBox[sb.getMatrix().getRowCount(), sb.getMatrix().getColumnCount()];
                ControlCollection col = panelInner.Controls; //.CopyTo(tb2,0);
                
                //Control c = col[0];
                //if ( c.GetType() == tb2.GetType() )
                //    tb2 = sb.getMatrix().getMatrix2TB();
                //sb.update(tb2);

                //panelInner.Controls.
            }
            return;
        }
    }
}